import json
import pymysql

# === 数据库配置 ===
conn = pymysql.connect(
    host='localhost',
    user='root',
    password='jintiankeji666@123',  # 改成你的密码
    database='cloud-android',
    charset='utf8mb4'
)
cursor = conn.cursor()

# === 创建表（如不存在） ===
cursor.execute("""
CREATE TABLE IF NOT EXISTS countries (
  name VARCHAR(255),
  code VARCHAR(10)
);
""")

# === 读取 JSON 文件 ===
with open('output.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# === 插入数据 ===
sql = "INSERT INTO country_codes(country_name, code) VALUES (%s, %s)"
cursor.executemany(sql, [(item["name"], item["code"]) for item in data])

conn.commit()
cursor.close()
conn.close()

print("✅ 导入完成，共插入", len(data), "条记录")
